package com.magina.antiPro.service.impl;


import com.alibaba.fastjson.JSONException;
import com.magina.antiPro.model.AccessToken;
import com.magina.antiPro.model.JSConfig;
import com.magina.antiPro.model.Members;
import com.magina.antiPro.model.WeiXinUser;
import com.magina.antiPro.service.WechatService;
import com.magina.antiPro.util.ProjectConst;
import com.magina.antiPro.util.QiYeWeiXinUtil;
import com.magina.antiPro.util.WeiXinUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service("wechatService")
public class WechatServiceImpl implements WechatService {

//	@Autowired
//	private WechatDao wechatDao;

    @Override
    public Members getMembersInfo(String code) throws JSONException {
        //1,获取access_token
        AccessToken accessToken = QiYeWeiXinUtil.access_token();
        String access_token = accessToken.getAccess_token();
        //2,获取UserId
        String UserId = QiYeWeiXinUtil.getUserId(access_token, code);
        //3,获取Members
        Members members = QiYeWeiXinUtil.getMembers(access_token, UserId);
        return members;
    }

    @Override
    public JSConfig getJSConfig(String url) {
        //获取参数为签名做准备
        AccessToken accessToken = QiYeWeiXinUtil.access_token();
        String jsapi_ticket = QiYeWeiXinUtil.jsapi_ticket(accessToken.getAccess_token());
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String nonce_str = UUID.randomUUID().toString();
        //url = "http://mp.weixin.qq.com?params=value";
        String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
        String signature = "";
        System.out.println(string1);
        //开始签名
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = QiYeWeiXinUtil.byteToHex(crypt.digest());
            System.out.println("signature:" + signature);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //封装返回数据
        JSConfig jsConfig = new JSConfig();
        jsConfig.setBeta(true);
        jsConfig.setDebug(true);
        jsConfig.setAppId("ww3c9853412b633936");
        jsConfig.setTimestamp(timestamp);
        jsConfig.setNonceStr(nonce_str);
        jsConfig.setSignature(signature);
        return jsConfig;
    }

    @Override
    public WeiXinUser getUserInfo(String accessToken, String openId) {
        WeiXinUser weixinUserInfo = null;
        // 拼接获取用户信息接口的请求地址
        String requestUrl = ProjectConst.GET_WEIXIN_USER_URL.replace("ACCESS_TOKEN", accessToken).replace(
                "OPENID", openId);
        // 获取用户信息(返回的是Json格式内容)
        JSONObject jsonObject = WeiXinUtils.doGetStr(requestUrl);

        if (null != jsonObject) {
            try {
                //封装获取到的用户信息
                weixinUserInfo = new WeiXinUser();
                // 用户的标识
                weixinUserInfo.setOpenId(jsonObject.getString("openid"));
                // 昵称
                weixinUserInfo.setNickname(jsonObject.getString("nickname"));
                // 用户的性别（1是男性，2是女性，0是未知）
                weixinUserInfo.setSex(jsonObject.getInt("sex"));
                // 用户所在国家
                weixinUserInfo.setCountry(jsonObject.getString("country"));
                // 用户所在省份
                weixinUserInfo.setProvince(jsonObject.getString("province"));
                // 用户所在城市
                weixinUserInfo.setCity(jsonObject.getString("city"));
                // 用户头像
                weixinUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
            } catch (Exception e) {
                if (0 == weixinUserInfo.getSubscribe()) {
                    System.out.println("用户并没有关注本公众号");
                } else {
                    int errorCode = jsonObject.getInt("errcode");
                    String errorMsg = jsonObject.getString("errmsg");
                    System.out.println("由于" + errorCode + "错误码；错误信息为：" + errorMsg + "；导致获取用户信息失败");
                }
            }
        }
        return weixinUserInfo;
    }

    /**
     * 进行用户授权，获取到需要的授权字段，比如openId
     *
     * @param code 识别得到用户id必须的一个值
     *             得到网页授权凭证和用户id
     * @return
     */
    @Override
    public Map<String, String> oauth2GetOpenid(String code) {
        //自己的配置appid（公众号进行查阅）
        String appid = ProjectConst.PROJECT_APPID;
        //自己的配置APPSECRET;（公众号进行查阅）
        String appsecret = ProjectConst.PROJECT_APPSECRET;
        //拼接用户授权接口信息
        String requestUrl = ProjectConst.GET_WEBAUTH_URL.replace("APPID", appid).replace("SECRET", appsecret).replace("CODE", code);
        //存储获取到的授权字段信息
        Map<String, String> result = new HashMap<String, String>();
        try {
            JSONObject OpenidJSONO = WeiXinUtils.doGetStr(requestUrl);
            //OpenidJSONO可以得到的内容：access_token expires_in  refresh_token openid scope
            String Openid = String.valueOf(OpenidJSONO.get("openid"));
            String AccessToken = String.valueOf(OpenidJSONO.get("access_token"));
            //用户保存的作用域
            String Scope = String.valueOf(OpenidJSONO.get("scope"));
            String refresh_token = String.valueOf(OpenidJSONO.get("refresh_token"));
            result.put("Openid", Openid);
            result.put("AccessToken", AccessToken);
            result.put("scope", Scope);
            result.put("refresh_token", refresh_token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取到微信用户的唯一的OpendID
     *
     * @param code 这是要获取OpendId的必须的一个参数
     * @return
     */
    @Override
    public Map<String, String> getAuthInfo(String code) {
        //进行授权验证，获取到OpenID字段等信息
        Map<String, String> result = oauth2GetOpenid(code);
        // 从这里可以得到用户openid
        String openId = result.get("Openid");

        return result;
    }

//
//	@Override
//	@Transactional
//	public int addLocation(LocationBean locationBean) {
//		int num = wechatDao.addLocation(locationBean);
//		return num;
//	}
}
